from ETP.models import Product,Job,Suite,Step
import logging
from ETP.commons.RequestHttp import RequestHttp
from ETP.commons.SeleniumUtils import SeleniumUtils

logger = logging.getLogger(__name__)
collect_logger = logging.getLogger("collect")

#执行器类
class Actuator:

    #运行job
    def run_job(self,job_id):
        pass

    #运行suite
    def run_suite(self,suite_id):
        suite = Suite.objects.get(id=suite_id)
        logger.info("*******************************************************")
        logger.info("***********************执行Suite：%s*****************"%suite.name)

        steps = Step.objects.filter(suite_id=suite_id).order_by('run_order')
        if suite.type == 'api':
            for step in steps:
                step_id = step.id
                self.excute_apiStep(step_id)
        else:
            for step in steps:
                step_id = step.id
                self.excute_uiStep(step_id)

        logger.info("***********************Suite:%s 执行完毕*****************"%suite.name)
        logger.info("*******************************************************")


    #运行step
    def excute_apiStep(self,step_id):
        logger.info("*******************************************************")
        step = Step.objects.get(id=step_id)
        logger.info("****************执行步骤%d***********************" %step.run_order)
        logger.info("测试URL：%s"%step.api_url)
        logger.info("测试Header：%s"%step.api_header)
        logger.info("请求方式：%s"%step.api_method)
        logger.info("请求json：%s"%step.api_json)
        logger.info("请求参数：%s"%step.api_params)
        logger.info("预期结果：%s" %step.api_expect)
        reqsth_ttp = RequestHttp()
        reqsth_ttp.run_api(step.id)



    def excute_uiStep(self,step_id):
        logger.info("*******************************************************")
        step = Step.objects.get(id=step_id)
        logger.info("******************执行步骤%d*****************"%step.run_order)
        logger.info("操作：%s"%step.ui_operation)
        logger.info("定位方式：%s"%step.ui_locate_method)
        logger.info("定位表达式：%s"%step.ui_locate_url)
        logger.info("定位表达式：%s"%step.ui_locate_url)
        logger.info("操作数据：%s"%step.ui_data)
        seleniumUtils = SeleniumUtils()
        seleniumUtils.run_ui(step.id)